package com.login;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.jdbcUtil.JDBCUtil;

public class LoginImpl {
  //查找是否存在由此账号
	public boolean serachUser(UserInfo user){
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try{
			conn=JDBCUtil.getConnection();
			String sql="select * from user where email='"+user.getEmail()+"';";
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()){
				return true;
			}
		}catch(Exception e){
			System.out.println(e);
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}
	//核对账户密码
	public boolean checkPw(UserInfo user){
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try{
			conn=JDBCUtil.getConnection();
			String sql="select * from user where email='"+user.getEmail()+"';";
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()){
				if(user.getPassword().equals(rs.getString("password"))){
					return true;
				}
			}
		}catch(Exception e){
			System.out.println(e);
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}
	public int getId(UserInfo user){
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try{
			conn=JDBCUtil.getConnection();
			String sql="select id from user where email='"+user.getEmail()+"' and password='"+user.getPassword()+"'";
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()){
				return rs.getInt("id");
			}
		}catch(Exception e){
			System.out.println(e);
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return -1;
	}
}
